﻿package classes{
	import flash.display.MovieClip;
	public class Ninja extends MovieClip {
		private static  const ST_IDLE = 0;
		private static  const ST_RUN = 1;
		private static  const ST_HIT = 2;
		private static  const ST_ATTACK = 3;
		private static  const ST_JUMP = 4;
		private static  const ST_DIED = 5;
		private static  const ST_SLASH = 6;

		public static  const D_LEFT = 4;
		public static  const D_RIGHT = 3;
		public var C_MAXSPEED = 15;
		public var MAX_HP:int = 150;
		public var MAX_MANA:int = 100;
		public var DAMAGE:int = 30;
		
		public var speed:int;
		public var HP:int;
		public var mana:int;
		private var currentState:int;
		private var currentDirection:int;
		private var nFrameLeft:int=-1;
		private var floorY:int = 320;
		private var v:int = 0;
		
		
		public function Ninja():void {
			Init();
		}
		public function Init()
		{
			speed = 0;
			currentState = ST_IDLE;
			gotoAndStop(1);
			trace("Ninja");
			HP=MAX_HP;
			mana = MAX_MANA;
		}
		public function Update():void {
			x+=speed;
			if (currentState == ST_RUN ) {

				
			}
			else if(currentState == ST_HIT)
			{
				SlowDown(8);
			}
			else if(currentState == ST_JUMP)
			{
				SlowDown(C_MAXSPEED/2);
				if(v > 0 && y>= floorY)
				{
					y == floorY;
					v == 0;
					Idle();
				}
				else
				{
					y+= v;
					v+=5;
				}
			}
			else if(currentState == ST_ATTACK)
			{
				SlowDown(C_MAXSPEED/3);		
			}else if(currentState == ST_SLASH)
			{
				SlowDown(1);		
			}
			else 
			{
				SlowDown(C_MAXSPEED/2);
			}
			if(nFrameLeft == 0 )
			{
				if(v > 0 && y>= floorY)
				{
					y == floorY;
					v == 0;
					Idle();
				}
				else
				{
					y+= v;
					v+=5;
					nFrameLeft++;
				}
			}
			nFrameLeft--;
		}
		public function Run(dir:int):void {
			
			if(currentState == ST_RUN)
			{
				if(currentDirection == dir)
				{
					return;
				}
			}
			else if(currentState == ST_JUMP)
			{
				currentDirection = dir;
				if (currentDirection == D_RIGHT) {
					scaleX = -Math.abs(scaleX);
				}
				else {
					scaleX = Math.abs(scaleX);
				}
				return;
			}
			else if(currentState != ST_IDLE)
			{
				return;
			}
			currentDirection = dir;
			if (currentDirection == D_RIGHT) {
				scaleX = -Math.abs(scaleX);
			}
			if (dir == D_LEFT) {
				scaleX = Math.abs(scaleX);
			}
			currentState = ST_RUN;
			gotoAndStop(2);
			
		}
		public function incMana(m:int)
		{
			
			if(currentState == ST_SLASH)
			{
				return;
			}
			mana += m;
			if(mana>MAX_MANA)
			{
				mana=MAX_MANA;
			}
		}
		public function Move(dx:int)
		{
			if(currentState != ST_RUN
			   && currentState != ST_JUMP)
				return;
			speed +=dx;
			if(speed > C_MAXSPEED)
			{
				speed = C_MAXSPEED;
			}
			if(speed < -C_MAXSPEED)
			{
				speed = -C_MAXSPEED;
			}			
		}
		public function StopRun()
		{
			if(currentState == ST_RUN)
			{
				Idle();
			}
		}
		private function SlowDown(d:int)
		{
			if(speed > d)
			{
				speed -= d;
			}
			else if(speed < -d)
			{
				speed += d;
			}
			else
			{
				speed = 0;
			}
		}
		public function Hit(dam:int):void {
			if(currentState == ST_HIT)
			{
				return;
			}
			currentState = ST_HIT;
			gotoAndStop(5);
			nFrameLeft = 2;
			HP -= dam;
			if(HP<=0)
			{
				v = -10;
				currentState = ST_DIED;		
				gotoAndStop(6);
			}
		}
		public function IsFire():Boolean{
			return (currentState == ST_ATTACK && nFrameLeft == 2)||
			currentState == ST_SLASH;
		}
		public function IsHitable()
		{
			return HP>0 && currentState != ST_SLASH;
		}
		public function GetAttackX():int{
			if(currentState == ST_SLASH)
				return x - 40*scaleX;
			return x - 20*scaleX;
		}
		public function Attack():void {
			if(currentState == ST_JUMP)
			{
				if(v < 0 || Math.abs(floorY-y)>= 100)
				{
					return;
				}
				else
				{
					y = floorY;
				}
			}
			else if(!(currentState == ST_IDLE ||
				 currentState == ST_RUN))
			{
				return;
			}
			currentState = ST_ATTACK;
			gotoAndStop(4);
			nFrameLeft = 4;
		}
		public function Slash():void {
			if(mana < 50)
			{
				return;
			}
			if(currentState == ST_JUMP)
			{
				if(v < 0 || Math.abs(floorY-y)>= 100)
				{
					return;
				}
				else
				{
					y = floorY;
				}
			}
			else if(!(currentState == ST_IDLE ||
				 currentState == ST_RUN))
			{
				return;
			}
			currentState = ST_SLASH;
			if (currentDirection == D_RIGHT) {
				speed = C_MAXSPEED+10;
			}
			else
			{
				speed = -C_MAXSPEED-10;
			}
			mana-=50;
			gotoAndStop(7);
			nFrameLeft = 10;
		}
		public function Jump():void {
			if(!(currentState == ST_IDLE || currentState == ST_RUN))
			{
				return;
			}
			currentState = ST_JUMP;
			gotoAndStop(3);
			v = -30;
//			nFrameLeft = 4;
		}
		public function Idle():void {
			if(currentState == ST_IDLE || currentState == ST_DIED)
			{
				return;
			}
			currentState = ST_IDLE;
			gotoAndStop(1);
		}
	}
}